import { api } from 'common/src/api/preparatoryGraduation/common';
import UTILS from "common/src/utils/utils";
import dayjs from 'dayjs';
import fetch from 'common/src/utils/fetch';
export default {
  data() {
    return {
      loading: false,
      source: {
        colleges: [],
        majors: [],
        status: [],
        sexes: []
      },
      defaultPage: {
        total: 0,
        pageNum_: 1,
        pageSize_: 20
      },
      page: {
        total: 0,
        pageNum_: 1,
        pageSize_: 20
      },
      tableData: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    this.initDictData();
  },
  methods: {
    initDictData() {
      this.source.status = [
        {
          key: 1,
          value: 1,
          label: '未结业'
        },
        {
          key: 2,
          value: 2,
          label: '已结业'
        },
        {
          key: 3,
          value: 3,
          label: '退回生源地'
        }
      ];
      this.initCollegeData();
      this.initSexData();
    },
    initCollegeData() {
      api.findCollege(this.$store.getters.language || "cn").then(res => {
        UTILS.fillSelect({
            colleges: "X_YX"
          },
          res.data,
          this.source
        );
        this.source.colleges = this.source.colleges.filter(item => item.value === '000637');
      });
    },
    initMajorData() {
      this.queryForm.major=null;
      this.source.majors=[];
      if (!this.queryForm.grade || !this.queryForm.college) {        
        return;
      }
      const params = {
        faculty: this.queryForm.college,
        grade: this.queryForm.grade,       
        pageNum_: 1,
        pageSize_: 200,
        projId: '1'
      };
      api.getMajorList(params).then(res => {
        const resultList = res.data && res.data.list || [];
        let options = [];
        resultList.forEach(item => {
          if (item.professionCode&&item.trainingLevel!='1') {
            options.push({
              key: item.professionCode,
              value: item.professionCode,
              label: item.professionName+"("+item.professionCode+")"
            })
          }
        })
        this.source.majors = options;
      });
    },
    initSexData() {
      api.findSex(this.$store.getters.language || "cn").then(res => {
        UTILS.fillSelect({
            sexes: "G_XBIE"
          },
          res.data,
          this.source
        );
      });
    },
    handleGradeChange() {
      this.initMajorData();
    },
    handleCollegeChange() {
      this.initMajorData();
    },
    triggingLoading() {
      this.loading = !this.loading;
    },
    //将日期转化为YYYY-MM-dd格式
    dateFormat(date = new Date()) {
      return this.timeFormate(date);
    },
    timeFormate(time, fmt = "YYYY-MM-DD") {
      return dayjs(time).format(fmt);
    },
    // 公共下载单文件方法
    downloadFile(objectKey, fileName) {
      return fetch({
        url: `/api/commonservice/obsfile/downloadfile?objectkey=${objectKey}`,
        method: 'get',
        responseType: "blob",
        timeout: 0
      }).then(res => {
        UTILS.downloadFile(
          res,
          fileName,
          fileName.substr(fileName.lastIndexOf(".") + 1)
        );
      });
    }
  }
};
